<?php

/**
 * @copyright 2011
 * @version $Id$
 * @access public
 */
class Default_Model_Facade_Login{

	/**
	 * Constructor de la clase
	 * @return void
	 */
	public function  __construct() {

	}

	public function createRandPass($pw_length = 8, $use_caps = true, $use_numeric = true, $use_specials = true) {
		$caps = array();
		$numbers = array();
		$num_specials = 0;
		$reg_length = $pw_length;
		$pws = array();
		$chars = range(97, 122); // create a-z
		if ($use_caps) $caps = range(65, 90); // create A-Z
		if ($use_numeric) $numbers = range(48, 57); // create 0-9
		$all = array_merge($chars, $caps, $numbers);
		if ($use_specials) {
			$reg_length =  ceil($pw_length*0.75);
			$num_specials = $pw_length - $reg_length;
			if ($num_specials > 5) $num_specials = 5;
			$signs = range(33, 47);
			$rs_keys = array_rand($signs, $num_specials);
			foreach ($rs_keys as $rs) {
				$pws[] = chr($signs[$rs]);
			}
		}
		$rand_keys = array_rand($all, $reg_length);
		foreach ($rand_keys as $rand) {
			$pw[] = chr($all[$rand]);
		}
		$compl = array_merge($pw, $pws);
		shuffle($compl);
		return implode('', $compl);
	}

	public function validateEmailExist($to=null){
		$validatorEmail		=new Zend_Validate_EmailAddress();
		$s_Sass     = new Zend_Session_Namespace('s_Sass');
		$validatorexistsEmail 	= new Zend_Validate_Db_RecordExists(array('table' => 'nanotec_users', 'field' => 'Email'));
		$valid=true;
		$errores=null;

		if(!$validatorEmail->isValid($to['Email'])){
			$errores[]="Error: Debe escribir un email válido.";
			$valid=false;
		}
		if(!$validatorexistsEmail->isValid($to['Email'])){
			$errores[]="Error: No hay ninguna cuenta con ese email, por favor, pruebe con otro.";
			$valid=false;
		}
		$s_Sass->messageError=$errores;
		return $valid;


	}
	public function RecoveryPassword($to=null){
		$TBD_Resource = new Default_Model_Login();
		try{
		
			$data = $TBD_Resource->RecoveryPassword($to);
		
			return $data;
		}catch(Exception $e)
		{
			$error=array($e->getFile(), 'Exception: '.$e->getMessage());
			$ExceptionHandler= new Default_Model_ExceptionHandler();
			$ExceptionHandler->saveErrors($error);
		}
		
	}
	


}